#include "modulehplcautosampler.h"
#include  <QDebug>
#include "protocol/hplc/qinfine/autosampler/hplcqinfineautosamplerprotocol.h"
#include "protocol/hplc/sparklink/autosampler/hplcsparklinkautosamplerprotocol.h"
#include "common/hplcqinfinedefine.h"
#include "modulehplcautosamplercmdid.h"

ModuleHplcAutoSampler::ModuleHplcAutoSampler(QObject *parent)
    : BaseModule (parent)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(BASE_TICK_MS);
    connect(m_timer, &QTimer::timeout, this, &ModuleHplcAutoSampler::onTimeout);
}

ModuleHplcAutoSampler::~ModuleHplcAutoSampler()
{
    delete  m_timer;
}

bool ModuleHplcAutoSampler::setConfig(int id, QVariant &var)
{

}

bool ModuleHplcAutoSampler::getConfig(int id, QVariant &var)
{

}

bool ModuleHplcAutoSampler::setParam(int id, QVariant &var)
{

}

bool ModuleHplcAutoSampler::getParam(int id, QVariant &var)
{

}

bool ModuleHplcAutoSampler::setProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->setProtocolCmd(id, var);
}

bool ModuleHplcAutoSampler::getProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->getProtocolCmd(id, var);
}

void ModuleHplcAutoSampler::setPort(BasePort *port)
{
    QMutexLocker locker(m_mutex);
    if (m_port)
    {
        m_port->disconnect();
        m_port->deleteLater();
    }
    m_port = port;

    connect(port, &BasePort::receivePackets,
            this, &ModuleHplcAutoSampler::onParse, Qt::QueuedConnection);
    connect(this, &ModuleHplcAutoSampler::requestSend,
            port, &BasePort::onSend, Qt::QueuedConnection);
}

BasePort *ModuleHplcAutoSampler::getPort(void)
{
    QMutexLocker locker(m_mutex);
    return m_port;
}

void ModuleHplcAutoSampler::setProtocol(BaseProtocol *protocol)
{
    QMutexLocker locker(m_mutex);
    if (m_protocol)
    {
        m_protocol->disconnect();
        m_protocol->deleteLater();
    }
    m_protocol = protocol;
    connect(m_protocol, &BaseProtocol::requestSend, this, &ModuleHplcAutoSampler::requestSend);
    connect(m_protocol, &BaseProtocol::parseCompleted, this, &ModuleHplcAutoSampler::parseCompleted);
}

BaseProtocol *ModuleHplcAutoSampler::getProtocol()
{
    QMutexLocker locker(m_mutex);
    return m_protocol;
}

void ModuleHplcAutoSampler::setProtocolAddress(quint8 address)
{
    QMutexLocker locker(m_mutex);
    m_protocol->setConfig(0, address);
}

quint8 ModuleHplcAutoSampler::getProtocolAddress(void)
{
    QMutexLocker locker(m_mutex);
    QVariant var;
    m_protocol->getConfig(0, var);
    return var.toUInt();
}

hplc::AutoSamplerParam *ModuleHplcAutoSampler::getParam()
{
    QMutexLocker locker(m_mutex);
    return &m_param;
}

void ModuleHplcAutoSampler::onTimeout()
{
    QMutexLocker locker(m_mutex);
    m_protocol->onBaseTickTimeout();
}

void ModuleHplcAutoSampler::onParse(QList<ProtocolPacket> packets)
{
    QMutexLocker locker(m_mutex);
    if (m_enable)
        m_protocol->onWork(packets);
}

void ModuleHplcAutoSampler::onStart()
{
    QMutexLocker locker(m_mutex);
//    if (m_enable && m_port->isOpen())
    {
        m_timer->start();
    }
}
